// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Use of the value returned by the constructor's `resolve` method.
esid: sec-promise.any
info: |
  Let result be PerformPromiseAny(iteratorRecord, C, promiseCapability).

  PerformPromiseAny

  Repeat
    ...
    i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
    ...
    r. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], rejectElement »).

features: [Promise.any]
---*/

function Test() {
    let originalCallCount = 0;
    let newCallCount = 0;
    let P = function(executor) {
      executor(function() {}, function() {});
    };
    P.resolve = function() {
      return newThenable;
    };

    let originalThenable = {
      then() {
        originalCallCount += 1;
      }
    };
    let newThenable = {
      then() {
        newCallCount += 1;
      }
    };

    Promise.any.call(P, [originalThenable]);

    assert.sameValue(originalCallCount, 0, 'original `then` method not invoked');
    assert.sameValue(newCallCount, 1, 'new `then` method invoked exactly once');
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
